﻿Imports System.Data.SqlClient
Imports extranet.BE
Imports MySql.Data.MySqlClient

Public Class DACategoriaVideo
  Dim objBD As New BancoDeDados
  Dim objDS As New DataSet
  Public objQuery As New BEQuery

  Sub New()
  End Sub

  Sub New(ByVal booCondicaoSimilar As Boolean)
    objQuery.Where.CondicaoSimilar = booCondicaoSimilar
  End Sub

  Public Sub Cadastra(ByRef objBECategoriaVideo As BECategoriaVideo)
    Try
      With objBECategoriaVideo
        .IdCategoria = _
        objBD.ExecutaEscalar("INSERT INTO ext_categoria_videos (categoria) " & _
                              "VALUES ('" & .Categoria & "');")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DACategoriaVideo.Cadastra(): " & ex.Message)
    End Try
  End Sub

  Public Sub Atualiza(ByVal objBECategoriaVideo As BECategoriaVideo)
    Try
      With objBECategoriaVideo
        objBD.ExecutaNonQuery("UPDATE ext_categoria_videos " & _
                              "SET categoria='" & .Categoria & "' " & _
                              "WHERE id_categoria=" & .IdCategoria & ";")
      End With
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DACategoriaVideo.Atualiza(): " & ex.Message)
    End Try
  End Sub

  Public Sub Exclui(ByVal objBECategoriaVideo As BECategoriaVideo)
    Try
      objBD.ExecutaNonQuery("DELETE FROM ext_categoria_videos WHERE id_categoria=" & objBECategoriaVideo.IdCategoria & ";")
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DACategoriaVideo.Exclui(): " & ex.Message)
    End Try
  End Sub

  Public Function SelecionaCategoriaVideos(ByVal objBECategoriaVideo As BECategoriaVideo) As BECategoriaVideo
    Try
      objQuery.Where.CondicaoSimilar = False
      Dim objBECategoriaVideos As List(Of BECategoriaVideo) = SelecionaCategoriasVideos(objBECategoriaVideo)
      If objBECategoriaVideos.Count > 0 Then
        objBECategoriaVideo = objBECategoriaVideos(0)
      Else
        objBECategoriaVideo = Nothing
      End If
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DACategoriaVideo.SelecionaCategoriaVideos(): " & ex.Message)
    End Try
    Return objBECategoriaVideo
  End Function

  Public Function SelecionaCategoriasVideos(ByVal objBECategoriaVideo As BECategoriaVideo) As List(Of BECategoriaVideo)
    Dim objBECategoriaVideos As List(Of BECategoriaVideo) = New List(Of BECategoriaVideo)
    Try
      objBD.duplicaAspasSimples(objBECategoriaVideo, GetType(BECategoriaVideo))
      objQuery._Select = "SELECT * FROM ext_categoria_videos"
      If objBECategoriaVideo IsNot Nothing Then
        If objBECategoriaVideo.IdCategoria > 0 Then objQuery.Where.AdicionarCondicaoWhere("id_categoria", objBECategoriaVideo.IdCategoria, False)
        objQuery.Where.AdicionarCondicaoWhere("categoria", objBECategoriaVideo.Categoria)
      End If
      objBECategoriaVideos = SelecionaCategoriasVideos()
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DACategoriaVideo.SelecionaCategoriaVideos(): " & ex.Message)
    End Try
    Return objBECategoriaVideos
  End Function

  Private Function SelecionaCategoriasVideos() As List(Of BECategoriaVideo)
    Dim objBECategoriasVideos As List(Of BECategoriaVideo) = New List(Of BECategoriaVideo)
    Try
      Using objSQLCommand As MySqlCommand = New MySqlCommand(objQuery.Query, BancoDeDados.ConnectionInstance())
        Using objSQLDReader As MySqlDataReader = objSQLCommand.ExecuteReader()
          While objSQLDReader.Read()
            objBECategoriasVideos.Add(New BECategoriaVideo(objSQLDReader("id_categoria"), _
                                                           objSQLDReader("categoria").ToString()))
          End While
        End Using
      End Using
    Catch ex As ApplicationException
      Throw New ApplicationException("extranet.DA.DACategoriaVideo.SelecionaCategoriaVideos(): " & ex.Message)
    End Try
    Return objBECategoriasVideos
  End Function

End Class
